<template>
    <el-dialog :title="title" :visible.sync="centerDialogVisible" width="40%" center>

        <div>
            <el-form style="width: 100%;">
                <el-form-item label="标题">
                    <el-input v-model='form.product_title'></el-input>
                </el-form-item>
                <el-form-item label="价格">
                    <el-input v-model='form.product_price'></el-input>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input v-model='form.product_dics'></el-input>
                </el-form-item>
                <el-form-item label="图片">
                    <el-upload action="/api/upImg" list-type="picture-card" :on-preview="handlePictureCardPreview"
                        :file-list="fileList" :limit="6" :on-success="onSuccess" :on-remove="handleRemove">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
            </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submit">确 定</el-button>
            <el-button @click="handleClose">取 消</el-button>
        </span>
    </el-dialog>

</template>
<script>
    import { insertProduct, getProduct, delProduct, upProduct } from '@/api/product.js'
    export default {
        data() {
            return {
                centerDialogVisible: false,
                dialogImageUrl: '',
                dialogVisible: false,
                fileList: [],

                form: {
                    product_title: '',
                    product_price: '',
                    prudout_img: '',
                    product_dics: ''
                },
                title: '',
                handelType: ''
            }
        },
        methods: {
            showDialog(option){
                this.handelType = option.type
                if (option.type === 'add') {
                    this.centerDialogVisible = true
                    this.title = '添加商品'
                    return
                }
                if (option.type === 'edit') {
                    this.title = '修改商品'
                    this.centerDialogVisible = true
                    this.form = option.rowData
                    return
                }
                
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },

            onSuccess(response, file, fileList) {
                this.form.prudout_img = response.data
                console.log("图片file：", file)
                console.log("图片fileList：", fileList)
            },
            submit() { // 提交商品
                const loading = this.$loading({
                    lock: true,
                    text: '加载中...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                if (this.handelType === 'add') {
                    this.insertProductFn()
                }
                if (this.handelType === 'edit') {
                    this.modifyProductFn()
                }
            },

            insertProductFn(){ // 添加商品操作
                insertProduct(this.form).then(res => {
                    this.loading().close();
                    this.centerDialogVisible = false
                    console.log("商品列表：", res)
                    this.$message({
                        showClose: true,
                        message: '恭喜你，这是一条成功消息',
                        type: 'success'
                    });
                    this.$parent.pruductInit()
                    this.dialogVisible = false;
                    this.reset()
                }).catch(err => {
                    this.loading().close();
                    console.log('失败：', err)
                })
            },

            modifyProductFn(){
                upProduct(this.form).then(res => {
                    this.loading().close();
                    this.centerDialogVisible = false
                    this.reset()
                    this.$parent.pruductInit()
                    this.$message({
                        type: 'success',
                        message: '编辑成功'
                    });
                }).catch(err => {
                    this.loading().close();
                    console.log('失败：', err)
                })
            },
            
            reset() {
                this.form = {
                    product_title: '',
                    product_price: '',
                    prudout_img: '',
                    product_dics: ''
                }
                this.dialogImageUrl = '';
                this.fileList = []
            },
            handleClose(){
                this.reset()
                this.centerDialogVisible = false
            },

            loading(){
               return this.$loading({
                    lock: true,
                    text: '加载中...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
            }
        },
    }
</script>